package com.app.micaihu.custom.view.dataview;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.app.micaihu.R;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.BaseOldActivity;
import com.app.micaihu.base.bean.DataBean;
import com.app.micaihu.bean.news.VideoEntity;
import com.app.micaihu.bean.news.VideoTranslate;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.configure.URIContant;
import com.app.micaihu.custom.view.CustomImageView;
import com.app.micaihu.listener.HttpListener;
import com.app.micaihu.utils.HttpUtils;
import com.app.micaihu.utils.MobclickAgentUtils;
import com.app.micaihu.view.newsdetail.VideoDetailActivity;
import com.app.micaihu.view.newsdetail.VideoShowActivity;
import com.app.utils.util.ToastUtils;
import com.app.utils.util.image.LoadImageUtils;
import com.baidu.mobstat.StatService;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by linkai on 2017/5/24.
 */
public class VideoChannelView1 extends LinearLayout implements View.OnClickListener {

    private String id;
    private String name;

    private View translate_top;
    private View translate_huan;
    private TextView translate_title;
    View firstlayout, secondlayout, threelayout, fourlayout;
    View firstLinesView, twoLinesView;

    TextView firsttitle, firstChannel, fistQishu;
    TextView secondtitle, secondChannel, secondQishu;
    TextView threetitle, threeChannel, threeQishu;
    TextView fourtitle, fourChannel, fourQishu;
    CustomImageView first_image, second_image, three_image, four_image;
    private TextView fourTime;
    private TextView threeTime;
    private TextView secondTime;
    private TextView fistTime;

    public VideoChannelView1(Context context) {
        super(context);
        inflate(context, R.layout.view_video_translate, this);
        init();
    }

    public VideoChannelView1(Context context, AttributeSet attrs) {
        super(context, attrs);
        inflate(context, R.layout.view_video_translate, this);
        init();
    }

    private void init() {
        translate_top = findViewById(R.id.translate_top);
        translate_huan = findViewById(R.id.translate_huan);
        translate_title = (TextView) findViewById(R.id.translate_title);
        findViewById(R.id.translate_more).setOnClickListener(this);
        translate_huan.setOnClickListener(this);
        firstlayout = findViewById(R.id.first_layout);
        secondlayout = findViewById(R.id.second_layout);
        threelayout = findViewById(R.id.three_layout);
        fourlayout = findViewById(R.id.four_layout);
        firstlayout.setOnClickListener(this);
        secondlayout.setOnClickListener(this);
        threelayout.setOnClickListener(this);
        fourlayout.setOnClickListener(this);

        firstLinesView = findViewById(R.id.translate_first);
        twoLinesView = findViewById(R.id.translate_two);

        firsttitle = (TextView) findViewById(R.id.first_title);
        firstChannel = (TextView) findViewById(R.id.first_channel);
        fistQishu = (TextView) findViewById(R.id.fist_qishu);
        fistTime = (TextView) findViewById(R.id.fist_time);
        first_image = (CustomImageView) findViewById(R.id.first_image);

        secondtitle = (TextView) findViewById(R.id.second_title);
        secondChannel = (TextView) findViewById(R.id.second_channel);
        secondQishu = (TextView) findViewById(R.id.second_qishu);
        secondTime = (TextView) findViewById(R.id.second_time);
        second_image = (CustomImageView) findViewById(R.id.second_image);

        threetitle = (TextView) findViewById(R.id.three_title);
        threeChannel = (TextView) findViewById(R.id.three_channel);
        threeQishu = (TextView) findViewById(R.id.three_qishu);
        threeTime = (TextView) findViewById(R.id.three_time);
        three_image = (CustomImageView) findViewById(R.id.three_image);

        fourtitle = (TextView) findViewById(R.id.four_title);
        fourChannel = (TextView) findViewById(R.id.four_channel);
        fourQishu = (TextView) findViewById(R.id.four_qishu);
        fourTime = (TextView) findViewById(R.id.four_time);
        four_image = (CustomImageView) findViewById(R.id.four_image);


    }


    public void setData(VideoTranslate videoTranslate) {
        if (videoTranslate == null) {
            this.setVisibility(View.GONE);
            return;
        }
        id = videoTranslate.getCateId();
        name = videoTranslate.getCateName();
        if (TextUtils.isEmpty(videoTranslate.getCateName())) {
            translate_top.setVisibility(View.GONE);
            translate_huan.setVisibility(View.GONE);
        } else {
            translate_title.setText(videoTranslate.getCateName());
            if (translate_top.getVisibility() == View.GONE) {
                translate_top.setVisibility(View.VISIBLE);
                translate_huan.setVisibility(View.VISIBLE);
            }
        }
        if (videoTranslate.getVideoList() == null || videoTranslate.getVideoList().size() == 0) {
            firstLinesView.setVisibility(View.GONE);
            twoLinesView.setVisibility(View.GONE);
        } else if (videoTranslate.getVideoList().size() >= 4) {
            VideoEntity videoEntity = videoTranslate.getVideoList().get(0);
            if (videoEntity != null) {
                if (firstLinesView.getVisibility() != View.VISIBLE) {
                    firstLinesView.setVisibility(View.VISIBLE);
                }
                if (videoEntity.getArticleThumb() != null && videoEntity.getArticleThumb().length >= 1) {
                    LoadImageUtils.getInstance().loadImage(first_image, videoEntity.getArticleThumb()[0]);
                }

                firsttitle.setText(videoEntity.getArticleTitle());
                firstChannel.setText(videoEntity.getClickNum());
                fistQishu.setText(videoEntity.getCommentNum());

                if (TextUtils.isEmpty(videoEntity.getPlayTime())) {
                    fistTime.setVisibility(View.GONE);
                } else {
                    fistTime.setVisibility(View.VISIBLE);
                    fistTime.setText(videoEntity.getPlayTime());
                }

                if (firstlayout.getVisibility() != View.VISIBLE) {
                    firstlayout.setVisibility(View.VISIBLE);
                }
                firstlayout.setTag(videoEntity);
            } else {
                firstlayout.setVisibility(View.INVISIBLE);
            }
            videoEntity = videoTranslate.getVideoList().get(1);
            if (videoEntity != null) {
                if (firstLinesView.getVisibility() != View.VISIBLE) {
                    firstLinesView.setVisibility(View.VISIBLE);
                }
                if (videoEntity.getArticleThumb() != null && videoEntity.getArticleThumb().length >= 1) {
                    LoadImageUtils.getInstance().loadImage(second_image, videoEntity.getArticleThumb()[0]);
                }
                secondtitle.setText(videoEntity.getArticleTitle());
                secondChannel.setText(videoEntity.getClickNum());
                secondQishu.setText(videoEntity.getCommentNum());

                if (TextUtils.isEmpty(videoEntity.getPlayTime())) {
                    secondTime.setVisibility(View.GONE);
                } else {
                    secondTime.setVisibility(View.VISIBLE);
                    secondTime.setText(videoEntity.getPlayTime());
                }

                if (secondlayout.getVisibility() != View.VISIBLE) {
                    secondlayout.setVisibility(View.VISIBLE);
                }
                secondlayout.setTag(videoEntity);
            } else {
                secondlayout.setVisibility(View.INVISIBLE);
            }
            videoEntity = videoTranslate.getVideoList().get(2);
            final TextView threetitle = (TextView) findViewById(R.id.three_title);
            if (videoEntity != null) {
                if (twoLinesView.getVisibility() != View.VISIBLE) {
                    twoLinesView.setVisibility(View.VISIBLE);
                }
                if (videoEntity.getArticleThumb() != null && videoEntity.getArticleThumb().length >= 1) {
                    LoadImageUtils.getInstance().loadImage(three_image, videoEntity.getArticleThumb()[0]);
                }

                threetitle.setText(videoEntity.getArticleTitle());
                threeChannel.setText(videoEntity.getClickNum());
                threeQishu.setText(videoEntity.getCommentNum());


                if (TextUtils.isEmpty(videoEntity.getPlayTime())) {
                    threeTime.setVisibility(View.GONE);
                } else {
                    threeTime.setVisibility(View.VISIBLE);
                    threeTime.setText(videoEntity.getPlayTime());
                }

                if (threelayout.getVisibility() != View.VISIBLE) {
                    threelayout.setVisibility(View.VISIBLE);
                }
                threelayout.setTag(videoEntity);
            } else {
                threelayout.setVisibility(View.INVISIBLE);
            }

            videoEntity = videoTranslate.getVideoList().get(3);
            if (videoEntity != null) {
                if (twoLinesView.getVisibility() != View.VISIBLE) {
                    twoLinesView.setVisibility(View.VISIBLE);
                }
                if (videoEntity.getArticleThumb() != null && videoEntity.getArticleThumb().length >= 1) {
                    LoadImageUtils.getInstance().loadImage(four_image, videoEntity.getArticleThumb()[0]);
                }
                fourtitle.setText(videoEntity.getArticleTitle());
                fourChannel.setText(videoEntity.getClickNum());
                TextView qishu = (TextView) findViewById(R.id.four_qishu);
                qishu.setText(videoEntity.getCommentNum());


                if (TextUtils.isEmpty(videoEntity.getPlayTime())) {
                    fourTime.setVisibility(View.GONE);
                } else {
                    fourTime.setVisibility(View.VISIBLE);
                    fourTime.setText(videoEntity.getPlayTime());
                }

                if (fourlayout.getVisibility() != View.VISIBLE) {
                    fourlayout.setVisibility(View.VISIBLE);
                }
                fourlayout.setTag(videoEntity);
            } else {
                fourlayout.setVisibility(View.INVISIBLE);
            }

        } else if (videoTranslate.getVideoList().size() == 3) {
            VideoEntity videoEntity = videoTranslate.getVideoList().get(0);
            if (videoEntity != null) {
                if (firstLinesView.getVisibility() != View.VISIBLE) {
                    firstLinesView.setVisibility(View.VISIBLE);
                }
                if (videoEntity.getArticleThumb() != null && videoEntity.getArticleThumb().length >= 1) {
                    LoadImageUtils.getInstance().loadImage(first_image, videoEntity.getArticleThumb()[0]);
                }

                firsttitle.setText(videoEntity.getArticleTitle());
                firstChannel.setText(videoEntity.getClickNum());
                fistQishu.setText(videoEntity.getCommentNum());
                if (TextUtils.isEmpty(videoEntity.getPlayTime())) {
                    fistTime.setVisibility(View.GONE);
                } else {
                    fistTime.setVisibility(View.VISIBLE);
                    fistTime.setText(videoEntity.getPlayTime());
                }
                if (firstlayout.getVisibility() != View.VISIBLE) {
                    firstlayout.setVisibility(View.VISIBLE);
                }
                firstlayout.setTag(videoEntity);
            } else {
                firstlayout.setVisibility(View.INVISIBLE);
            }
            videoEntity = videoTranslate.getVideoList().get(1);
            if (videoEntity != null) {
                if (firstLinesView.getVisibility() != View.VISIBLE) {
                    firstLinesView.setVisibility(View.VISIBLE);
                }
                if (videoEntity.getArticleThumb() != null && videoEntity.getArticleThumb().length >= 1) {
                    LoadImageUtils.getInstance().loadImage(second_image, videoEntity.getArticleThumb()[0]);
                }
                secondtitle.setText(videoEntity.getArticleTitle());
                secondChannel.setText(videoEntity.getClickNum());
                secondQishu.setText(videoEntity.getCommentNum());

                if (TextUtils.isEmpty(videoEntity.getPlayTime())) {
                    secondTime.setVisibility(View.GONE);
                } else {
                    secondTime.setVisibility(View.VISIBLE);
                    secondTime.setText(videoEntity.getPlayTime());
                }

                if (secondlayout.getVisibility() != View.VISIBLE) {
                    secondlayout.setVisibility(View.VISIBLE);
                }
                secondlayout.setTag(videoEntity);
            } else {
                secondlayout.setVisibility(View.INVISIBLE);
            }
            videoEntity = videoTranslate.getVideoList().get(2);
            if (videoEntity != null) {
                if (twoLinesView.getVisibility() != View.VISIBLE) {
                    twoLinesView.setVisibility(View.VISIBLE);
                }
                fourlayout.setVisibility(View.INVISIBLE);
                if (videoEntity.getArticleThumb() != null && videoEntity.getArticleThumb().length >= 1) {
                    LoadImageUtils.getInstance().loadImage(three_image, videoEntity.getArticleThumb()[0]);
                }
                threetitle.setText(videoEntity.getArticleTitle());
                threeChannel.setText(videoEntity.getClickNum());

                threeQishu.setText(videoEntity.getCommentNum());

                if (TextUtils.isEmpty(videoEntity.getPlayTime())) {
                    threeTime.setVisibility(View.GONE);
                } else {
                    threeTime.setVisibility(View.VISIBLE);
                    threeTime.setText(videoEntity.getPlayTime());
                }

                if (threelayout.getVisibility() != View.VISIBLE) {
                    threelayout.setVisibility(View.VISIBLE);
                }
                threelayout.setTag(videoEntity);
            } else {
                threelayout.setVisibility(View.INVISIBLE);
                fourlayout.setVisibility(View.INVISIBLE);
            }

        } else if (videoTranslate.getVideoList().size() == 1) {
            VideoEntity videoEntity = videoTranslate.getVideoList().get(0);
            if (twoLinesView.getVisibility() == View.VISIBLE) {
                twoLinesView.setVisibility(View.GONE);
            }
            if (videoEntity != null) {
                if (firstLinesView.getVisibility() != View.VISIBLE) {
                    firstLinesView.setVisibility(View.VISIBLE);
                }
                secondlayout.setVisibility(View.INVISIBLE);
                if (videoEntity.getArticleThumb() != null && videoEntity.getArticleThumb().length >= 1) {
                    LoadImageUtils.getInstance().loadImage(first_image, videoEntity.getArticleThumb()[0]);
                }
                firsttitle.setText(videoEntity.getArticleTitle());
                firstChannel.setText(videoEntity.getClickNum());

                fistQishu.setText(videoEntity.getCommentNum());

                if (TextUtils.isEmpty(videoEntity.getPlayTime())) {
                    fistTime.setVisibility(View.GONE);
                } else {
                    fistTime.setVisibility(View.VISIBLE);
                    fistTime.setText(videoEntity.getPlayTime());
                }
                if (firstlayout.getVisibility() != View.VISIBLE) {
                    firstlayout.setVisibility(View.VISIBLE);
                }
                firstlayout.setTag(videoEntity);
            } else {
                firstlayout.setVisibility(View.INVISIBLE);
                secondlayout.setVisibility(View.INVISIBLE);
            }

        } else if (videoTranslate.getVideoList().size() == 2) {
            VideoEntity videoEntity = videoTranslate.getVideoList().get(0);
            if (twoLinesView.getVisibility() == View.VISIBLE) {
                twoLinesView.setVisibility(View.GONE);
            }
            if (videoEntity != null) {
                if (firstLinesView.getVisibility() != View.VISIBLE) {
                    firstLinesView.setVisibility(View.VISIBLE);
                }
                if (videoEntity.getArticleThumb() != null && videoEntity.getArticleThumb().length >= 1) {
                    LoadImageUtils.getInstance().loadImage(first_image, videoEntity.getArticleThumb()[0]);
                }
                firsttitle.setText(videoEntity.getArticleTitle());
                firstChannel.setText(videoEntity.getClickNum());

                fistQishu.setText(videoEntity.getCommentNum());

                if (TextUtils.isEmpty(videoEntity.getPlayTime())) {
                    fistTime.setVisibility(View.GONE);
                } else {
                    fistTime.setVisibility(View.VISIBLE);
                    fistTime.setText(videoEntity.getPlayTime());
                }
                if (firstlayout.getVisibility() != View.VISIBLE) {
                    firstlayout.setVisibility(View.VISIBLE);
                }
                firstlayout.setTag(videoEntity);
            } else {
                firstlayout.setVisibility(View.INVISIBLE);
            }
            videoEntity = videoTranslate.getVideoList().get(1);
            if (videoEntity != null) {
                if (videoEntity.getArticleThumb() != null && videoEntity.getArticleThumb().length >= 1) {
                    LoadImageUtils.getInstance().loadImage(second_image, videoEntity.getArticleThumb()[0]);
                }
                secondtitle.setText(videoEntity.getArticleTitle());
                secondChannel.setText(videoEntity.getClickNum());

                secondQishu.setText(videoEntity.getCommentNum());

                if (TextUtils.isEmpty(videoEntity.getPlayTime())) {
                    secondTime.setVisibility(View.GONE);
                } else {
                    secondTime.setVisibility(View.VISIBLE);
                    secondTime.setText(videoEntity.getPlayTime());
                }

                if (secondlayout.getVisibility() != View.VISIBLE) {
                    secondlayout.setVisibility(View.VISIBLE);
                }
                secondlayout.setTag(videoEntity);
            } else {
                secondlayout.setVisibility(View.INVISIBLE);
            }

        }
    }

    @Override
    public void onClick(View v) {

        if (v == null) {
            return;
        }

        switch (v.getId()) {
            case R.id.translate_huan://换一批
                changData();
                break;
            case R.id.translate_more://更多
                if (getContext() == null) {
                    return;
                }
                Intent intent = new Intent(getContext(), VideoShowActivity.class);
                intent.putExtra(Config.Extra.PARAMETER1, id);
                getContext().startActivity(intent);
                break;
            case R.id.first_layout:
            case R.id.second_layout:
            case R.id.three_layout:
            case R.id.four_layout:
                if (getContext() == null) {
                    return;
                }
                VideoEntity videoEntity = (VideoEntity) v.getTag();
                intent = new Intent();
                intent.setClass(getContext(), VideoDetailActivity.class);
                intent.putExtra(Config.Extra.PARAMETER1, videoEntity.getArticleId());
                intent.putExtra(Config.Extra.PARAMETER2, TypeConstant.SOURCETYPE_NORMAL);
                getContext().startActivity(intent);
                if (TextUtils.isEmpty(name)) {
                    MobclickAgentUtils.event(Config.EventBauduId.game_076, "顶部视频点击");
                    StatService.onEvent(AppApplication.getInstance(), "067", "顶部视频点击", 1);//统计事件的点击次数
                } else {
                    MobclickAgentUtils.event(Config.EventBauduId.game_076, name + "视频点击");
                    StatService.onEvent(AppApplication.getInstance(), "067", name + "视频点击", 1);//统计事件的点击次数
                }
                break;
        }
    }

    private void changData() {
        Map<String, String> parms = new HashMap<>();
        parms.put("cateId", id + "");
        HttpListener<DataBean<List<VideoEntity>>> httpListener = new HttpListener<DataBean<List<VideoEntity>>>() {
            @Override
            public void onStart() {
            }

            @Override
            public void onError(VolleyError error) {
                ToastUtils.makeText("网络错误,请稍后再试");
            }

            @Override
            public void onSuccess(DataBean<List<VideoEntity>> response) {

                if (response.noErrorData()) {
                    if (response.getData().size() > 0) {
                        VideoTranslate videoTranslate = new VideoTranslate();
                        videoTranslate.setCateId(id);
                        videoTranslate.setCateName(name);
                        videoTranslate.setVideoList(response.getData());
                        setData(videoTranslate);
                    } else {
                        ToastUtils.makeText("没有数据了");
                    }
                } else {
                    ToastUtils.makeText(response.getNnderstoodMsg());
                }

            }
        };
        HttpUtils.addPostCacheRequest(URIContant.VIDEO_ROOM_LIST_CHANGE, new TypeToken<DataBean<List<VideoEntity>>>() {
        }.getType(), "", parms, httpListener);
    }
}
